﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NHibernate.Tool.hbm2ddl;
using com.pro2e.web.config.init;
using NHibernate;
using com.pro2e.console.service.auth.vo;
using NHibernate.Mapping;
using com.pro2e.console.core.hibernate;
using com.pro2e.console.service.auth;
using System.Collections.Generic;
using com.pro2e.console.service.system.vo;
using com.pro2e.console.service.system;
using com.pro2e.console.core.valueobject;
using com.pro2e.console.service.msg.vo;
using NHibernate.Proxy;
using com.pro2e.console.generic.util;
using System.Threading;
using Spring.Objects;
using sexhealth.console.service.post.vo;
using sexhealth.console.service.post;


public partial class Schema : System.Web.UI.Page
{

    AuthService authService = new AuthService();
    SystemService systemService = new SystemService();
    log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    DaoImp dao = new DaoImp();

    NHibernateSqlHelper sqlHelper = new NHibernateSqlHelper();

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SchemaExport export = new SchemaExport(WebApplication.Configuration);
        export.Create(false, true);
        lblStatus.Text += "<br/>初始化資料庫表格..完成";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            SchemaExport export = new SchemaExport(WebApplication.Configuration);
            export.Drop(false, true);
            lblStatus.Text = "刪除資料庫表格..完成";
        }
        catch (Exception ex)
        {
            lblStatus.Text = ex.Message;
        }

    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        SchemaExport export = new SchemaExport(WebApplication.Configuration);
        export.SetOutputFile("d:\\auth.ddl");
        export.Create(true, false);
        lblStatus.Text = "Schema genenrated success";
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        initAdmin();





    }

    private void grantAuth()
    {
        LoginRole role = dao.DaoGetVOById<LoginRole>(1);
        LoginUser user = dao.DaoGetVOById<LoginUser>("admin");

        user.BelongRoles = new List<LoginRole>();
        user.BelongRoles.Add(role);
        authService.DaoUpdate(user);

        role.MenuFuncs = authService.GetNotTopMenuFunc();
        authService.DaoUpdate(role);
    }

    private void initAdmin()
    {
        LoginUser user = new LoginUser();
        LoginRole role = new LoginRole();
        user.UserId = "admin";
        user.Password = "1234";
        user.FullNameInChinese = "系統管理者";
        user.FullNameInEnglish = "Administrator";
        authService.AddLoginUser(user);

        role.RoleName = "系統管理者";
        authService.AddRole(role);

        user.BelongRoles = new List<LoginRole>();
        user.BelongRoles.Add(role);
        authService.DaoUpdate(user);

        //List<LoginUser> userList = new List<LoginUser>();
        //List<LoginRole> roleList = new List<LoginRole>();

        //userList.Add(user);
        //roleList.Add(role);

        //user.BelongRoles = roleList;
        //role.BelongUsers = userList;

        //authService.UpdateRole(role);
        //authService.UpdateUser(user);
        lblStatus.Text += "<br>初始化系統使用者資料..完成";
    }

    private void createMenu()
    {
        ISession session = NHibernateSessionManager.Instance.GetSession();

        session.BeginTransaction();

        //網站內容管理
        MenuFunc webContentFunc_0 = new MenuFunc("網站首頁管理", null);
        webContentFunc_0.ListOrder = 100;
        session.Save(webContentFunc_0);
        MenuFunc webContentFunc_1 = new MenuFunc("性福知識+", null);
        webContentFunc_1.ListOrder = 200;
        session.Save(webContentFunc_1);
        MenuFunc webContentFunc_2 = new MenuFunc("性福守護站", null);
        webContentFunc_2.ListOrder = 300;
        session.Save(webContentFunc_2);
        MenuFunc webContentFunc_3 = new MenuFunc("性福影音站", null);
        webContentFunc_3.ListOrder = 400;
        session.Save(webContentFunc_3);
        MenuFunc webContentFunc_4 = new MenuFunc("常見問題", null);
        webContentFunc_4.ListOrder = 500;
        session.Save(webContentFunc_4);
        MenuFunc webContentFunc_5 = new MenuFunc("性福好站連結", null);
        webContentFunc_5.ListOrder = 600;
        session.Save(webContentFunc_5);


        //UC01 網站基本資料
        MenuFunc m0101 = new MenuFunc("網站首頁圖片", webContentFunc_0, "");
        MenuFunc m0102 = new MenuFunc("版權聲明與使用規定", webContentFunc_0, "");
        MenuFunc m0103 = new MenuFunc("隱私保護政策", webContentFunc_0, "");
        MenuFunc m0104 = new MenuFunc("網站首頁內容(中左)", webContentFunc_0, "");
        MenuFunc m0105 = new MenuFunc("網站首頁內容(中右)", webContentFunc_0, "");
        m0101.MainPath = "admin/UC01/0101indexpagepic.aspx";
        m0102.MainPath = "admin/UC01/0102copyright.aspx";
        m0103.MainPath = "admin/UC01/0103privacy.aspx";
        m0104.MainPath = "admin/UC01/0104indexpagecontent.aspx";
        m0105.MainPath = "admin/UC01/0105indexpagecontent.aspx";
        session.Save(m0101);
        session.Save(m0102);
        session.Save(m0103);
        session.Save(m0104);
        session.Save(m0105);
        //UC02性福知識+
        MenuFunc m0201 = new MenuFunc("分類文章維護", webContentFunc_1, "");
        MenuFunc m0202 = new MenuFunc("子分類維護", webContentFunc_1, "");
        m0201.MainPath = "admin/UC02/0201postmaintain.aspx";
        m0202.MainPath = "admin/UC02/0202channelmaintain.aspx";
        session.Save(m0201);
        session.Save(m0202);
        //UC03性福守護站
        MenuFunc m0301 = new MenuFunc("分類文章維護", webContentFunc_2, "");
        MenuFunc m0302 = new MenuFunc("子分類維護", webContentFunc_2, "");
        m0301.MainPath = "admin/UC03/0301postmaintain.aspx";
        m0302.MainPath = "admin/UC03/0302channelmaintain.aspx";
        session.Save(m0301);
        session.Save(m0302);
        //UC04常見問題
        MenuFunc m0401 = new MenuFunc("分類文章維護", webContentFunc_4, "");
        MenuFunc m0402 = new MenuFunc("子分類維護", webContentFunc_4, "");
        m0401.MainPath = "admin/UC04/0401postmaintain.aspx";
        m0402.MainPath = "admin/UC04/0402channelmaintain.aspx";
        session.Save(m0401);
        session.Save(m0402);
        //UC05性福影音站
        MenuFunc m0501 = new MenuFunc("影片分享維護", webContentFunc_3, "");
        MenuFunc m0502 = new MenuFunc("廣播分享維護", webContentFunc_3, "");
        m0501.MainPath = "admin/UC05/0501videomaintain.aspx";
        m0502.MainPath = "admin/UC05/0502boardcastmaintain.aspx";
        session.Save(m0501);
        session.Save(m0502);
        //UC06性福好站連結
        MenuFunc m0601 = new MenuFunc("連結內容維護", webContentFunc_5, "");
        m0601.MainPath = "admin/UC06/0601linkmaintain.aspx";
        session.Save(m0601);






        lblStatus.Text += "<br>目錄建立..完成";

        //UC14權限管理
        MenuFunc menuFunc = new MenuFunc("權限管理", null);
        menuFunc.ListOrder = 9999;
        session.Save(menuFunc);
        lblStatus.Text += "<br>初始化權限管理目錄..完成";



        //UC14
        MenuFunc m1401 = new MenuFunc("帳號管理", menuFunc, "");
        MenuFunc m1402 = new MenuFunc("群組管理", menuFunc, "");
        MenuFunc m1403 = new MenuFunc("帳號群組設定", menuFunc, "");
        MenuFunc m1404 = new MenuFunc("群組權限設定", menuFunc, "");
        MenuFunc m1405 = new MenuFunc("使用紀錄", menuFunc, "");


        m1401.MainPath = "admin/UC14/UserAdd.aspx";
        m1402.MainPath = "admin/UC14/RoleAdd.aspx";
        m1403.MainPath = "admin/UC14/UserRoleSet.aspx";
        m1404.MainPath = "admin/UC14/RoleFuncSet.aspx";
        m1405.MainPath = "admin/UC14/QueryLog.aspx";


        session.Save(m1401);
        session.Save(m1402);
        session.Save(m1403);
        session.Save(m1404);
        session.Save(m1405);


    }

    private IList<FunctionPath> GetFunctionPath(MenuFunc m, string[] paths)
    {
        IList<FunctionPath> flist = new List<FunctionPath>();
        foreach (string p in paths)
        {
            FunctionPath fp = new FunctionPath();
            fp.BelongMenuFunc = m;
            fp.Path = p;
            dao.DaoInsert(fp);
            flist.Add(fp);
        }
        return flist;
    }

    private void AddFunctionPath(ISession session, MenuFunc m1, string p)
    {
        FunctionPath f1 = new FunctionPath();
        f1.BelongMenuFunc = m1;
        f1.Path = p;
        session.Save(f1);
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        LoginRole role = dao.DaoGetVOById<LoginRole>(1);


        UserMenuFuncContainer.GetInstance().ResetAll();

    }

    protected void Button10_Click(object sender, EventArgs e)
    {
        UserMenuFuncContainer.GetInstance().ReloadAllMenu();
    }


    protected void Button8_Click(object sender, EventArgs e)
    {
        createMenu();

    }
    protected void Button9_Click(object sender, EventArgs e)
    {
        grantAuth();
    }


    protected void Button10_Click1(object sender, EventArgs e)
    {
        PostService postService = new PostService();
        ChannelVO ch = new ChannelVO();
        ch.Name = "網站首頁";
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "版權聲明與使用規定";
        ch.ParentId = 1;
        ch.LeftTitlePicPath = "../image/l-tt2.gif";
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "隱私權政策";
        ch.ParentId = 1;
        ch.LeftTitlePicPath = "../image/l-tt3.gif";
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "性福知識+";
        ch.LeftTitlePicPath = "../image/l-tt4.gif";
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "性福守護站";
        ch.LeftTitlePicPath = "../image/l-tt5.gif";
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "性福影音站";
        ch.LeftTitlePicPath = "../image/l-tt7.gif";
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "常見問題";
        ch.LeftTitlePicPath = "../image/l-tt1.gif";
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "性福好站連結";
        ch.LeftTitlePicPath = "../image/l-tt6.gif";
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "最新訊息";
        ch.ParentId = 4;
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "醫師專欄";
        ch.ParentId = 4;
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "相關新聞";
        ch.ParentId = 4;
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "網站首頁圖片";
        ch.ParentId = 1;
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "網站首頁內容(中左)";
        ch.ParentId = 1;
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "影片分享";
        ch.ParentId = 6;
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "廣播分享";
        ch.ParentId = 6;
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "2012天天真性福手冊";
        ch.ParentId = 5;
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "2011性福365手冊";
        ch.ParentId = 5;
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "常見問題FAQ";
        ch.ParentId = 7;
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "網站首頁內容(中右)";
        ch.ParentId = 1;
        postService.DaoInsert(ch);
        ch = new ChannelVO();
        ch.Name = "性福好站連結";
        ch.ParentId = 8;
        postService.DaoInsert(ch);
          ch = new ChannelVO();
          ch.Name = "什麼是勃起功能障礙";
        ch.ParentId = 1;
        postService.DaoInsert(ch);
        
        lblStatus.Text += "<br>建立網站初始資料..完成";
    }
}


